/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.datasource.pooled;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Clinton Begin
 */
/**
 * 池状态
 */
public class PoolState {
	/**
	 * 数据源
	 */
	protected PooledDataSource dataSource;

	/**
	 * 空闲连接
	 */
	protected final List<PooledConnection> idleConnections = new ArrayList<PooledConnection>();
	/**
	 * 当前活动连接
	 */
	protected final List<PooledConnection> activeConnections = new ArrayList<PooledConnection>();
	// ----------以下是一些统计信息----------
	/**
	 * 请求次数
	 */
	protected long requestCount = 0;
	/**
	 * 请求获得连接所需时间
	 */
	protected long accumulatedRequestTime = 0;
	/**
	 * 统计连接使用时间
	 */
	protected long accumulatedCheckoutTime = 0;
	/**
	 * 统计过期回收连接数
	 */
	protected long claimedOverdueConnectionCount = 0;
	/**
	 * 统计连接过期使用时间
	 */
	protected long accumulatedCheckoutTimeOfOverdueConnections = 0;
	/**
	 * 统计获取连接需要等待的时间
	 */
	protected long accumulatedWaitTime = 0;
	/**
	 * 统计获取连接需要等待的次数
	 */
	protected long hadToWaitCount = 0;
	/**
	 * 统计无效连接个数
	 */
	protected long badConnectionCount = 0;

	/**
	 * 数据源
	 * 
	 * @return
	 */
	public PoolState(PooledDataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * 请求次数
	 * 
	 * @return
	 */
	public synchronized long getRequestCount() {
		return requestCount;
	}

	/**
	 * 请求时间的平均值
	 * 
	 * @return
	 */
	public synchronized long getAverageRequestTime() {
		return requestCount == 0 ? 0 : accumulatedRequestTime / requestCount;
	}

	/**
	 * 总等待时间的平均值
	 * 
	 * @return
	 */
	public synchronized long getAverageWaitTime() {
		return hadToWaitCount == 0 ? 0 : accumulatedWaitTime / hadToWaitCount;

	}

	/**
	 * 要等待的次数
	 * 
	 * @return
	 */
	public synchronized long getHadToWaitCount() {
		return hadToWaitCount;
	}

	/**
	 * 坏的连接次数
	 * 
	 * @return
	 */
	public synchronized long getBadConnectionCount() {
		return badConnectionCount;
	}

	/**
	 * 最大可回收时间
	 * 
	 * @return
	 */
	public synchronized long getClaimedOverdueConnectionCount() {
		return claimedOverdueConnectionCount;
	}

	/**
	 * 过期回收时间的平均值
	 * 
	 * @return
	 */
	public synchronized long getAverageOverdueCheckoutTime() {
		return claimedOverdueConnectionCount == 0 ? 0
				: accumulatedCheckoutTimeOfOverdueConnections / claimedOverdueConnectionCount;
	}

	/**
	 * 过期回收时间的平均值
	 * 
	 * @return
	 */
	public synchronized long getAverageCheckoutTime() {
		return requestCount == 0 ? 0 : accumulatedCheckoutTime / requestCount;
	}

	/**
	 * 空闲的连接数量
	 * 
	 * @return
	 */
	public synchronized int getIdleConnectionCount() {
		return idleConnections.size();
	}

	/**
	 * 活动的连接的数量
	 * 
	 * @return
	 */
	public synchronized int getActiveConnectionCount() {
		return activeConnections.size();
	}

	// 打印统计信息，可以供性能优化用
	@Override
	public synchronized String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("\n===CONFINGURATION==============================================");
		builder.append("\n jdbcDriver                     ").append(dataSource.getDriver());
		builder.append("\n jdbcUrl                        ").append(dataSource.getUrl());
		builder.append("\n jdbcUsername                   ").append(dataSource.getUsername());
		builder.append("\n jdbcPassword                   ")
				.append((dataSource.getPassword() == null ? "NULL" : "************"));
		builder.append("\n poolMaxActiveConnections       ").append(dataSource.poolMaximumActiveConnections);
		builder.append("\n poolMaxIdleConnections         ").append(dataSource.poolMaximumIdleConnections);
		builder.append("\n poolMaxCheckoutTime            ").append(dataSource.poolMaximumCheckoutTime);
		builder.append("\n poolTimeToWait                 ").append(dataSource.poolTimeToWait);
		builder.append("\n poolPingEnabled                ").append(dataSource.poolPingEnabled);
		builder.append("\n poolPingQuery                  ").append(dataSource.poolPingQuery);
		builder.append("\n poolPingConnectionsNotUsedFor  ").append(dataSource.poolPingConnectionsNotUsedFor);
		builder.append("\n ---STATUS-----------------------------------------------------");
		builder.append("\n activeConnections              ").append(getActiveConnectionCount());
		builder.append("\n idleConnections                ").append(getIdleConnectionCount());
		builder.append("\n requestCount                   ").append(getRequestCount());
		builder.append("\n averageRequestTime             ").append(getAverageRequestTime());
		builder.append("\n averageCheckoutTime            ").append(getAverageCheckoutTime());
		builder.append("\n claimedOverdue                 ").append(getClaimedOverdueConnectionCount());
		builder.append("\n averageOverdueCheckoutTime     ").append(getAverageOverdueCheckoutTime());
		builder.append("\n hadToWait                      ").append(getHadToWaitCount());
		builder.append("\n averageWaitTime                ").append(getAverageWaitTime());
		builder.append("\n badConnectionCount             ").append(getBadConnectionCount());
		builder.append("\n===============================================================");
		return builder.toString();
	}

}
